import os
import logging
from logging.handlers import RotatingFileHandler
from datetime import datetime

def setup_logger(path_dir='logs', name='fdt_server'):
    log_dir = path_dir
    if not os.path.exists(log_dir):
        os.makedirs(log_dir)
    log_filename = os.path.join(log_dir, f'{name}.log')
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

    handler = RotatingFileHandler(log_filename, maxBytes=5*1024*1024, backupCount=2, encoding='utf-8')
    handler.setLevel(logging.INFO)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    # 控制台也输出
    console = logging.StreamHandler()
    console.setFormatter(formatter)
    console.setLevel(logging.DEBUG)
    logger.addHandler(console)
    return logger


setup_logger()

def getLogger():
    logger = logging.getLogger("fdt_server")
    return logger
